//改变验证码函数
//function changeCode() {
//
//	codeImg.src = httpAdress + "/servlet/validateCodeServlet?t=" + genTimestamp();
//
//}
//时间戳函数
//function genTimestamp() {
//	var time = new Date();
//	return time.getTime();
//
//}
console.log(localStorage.getItem("abc"))
function mtime(na){
	var num=60;
 var ff=setInterval(function(){
		num--;
		na.disabled=true;
		na.innerHTML=num+'s后尝试';
		na.style.background='gray';
		if(num==0){
			clearInterval(ff);
			na.disabled=false;
			na.innerHTML='获取验证码';
		na.style.background='#F26100';
		}
	},1000)
}

	var codeImg = document.getElementById("codeImg");
	//获取图形验证码
//	changeCode();
//	codeImg.addEventListener('click', function(event) {
//		changeCode();
//	});
	var regButton = document.getElementById('reg');//注册按钮
	var accountBox = document.getElementById('account');//手机号
	var reg_password = document.getElementById('password');//密码
	var pagenum = document.getElementById('pagenum');//图形码
	var checknum = document.getElementById('checknum');//验证码
	var please = document.getElementById('please');//邀请码
    var mhide = document.getElementById('hide');
	var forgetButton = document.getElementById('forgetPassword');
	var change = true;
	mhide.addEventListener('tap', function(event) {
		if(change) {
			mhide.src = '../../img/login/btn_eye_on@3x.png';
			reg_password.type = 'email';
			change = false;
		} else {
			mhide.src = '../../img/login/btn_eye@3x.png';
			reg_password.type = 'password';
			change = true;
		}
	});
	var getChecknum = document.getElementById('get-checknum');
	 getChecknum.addEventListener('touchstart',function(event){
    	getChecknum.style.background='#F0AD4E'
    });
    getChecknum.addEventListener('touchend',function(event){
    	getChecknum.style.background='#F26100'
    });
	getChecknum.addEventListener('tap', function(event) {
		var network = true;
		if(network) {
			if(accountBox == "") {
				mui.toast("手机号不能为空");
				return;
			}
			var check_phone_number = /^1([38]\d|4[57]|5[0-35-9]|7[06-8]|8[89])\d{8}$/;
			if(accountBox.value.length == 0) {
				mui.toast("手机号不能为空");
				return;
			}
			if(accountBox.value.length != 11) {
				mui.toast("请输入有效的手机号！");
				return;
			}
			if(!accountBox.value.match(check_phone_number)) {
				mui.toast("请输入有效的手机号！");
				return;
			}
		}
		//密码校验
		var reg_password = document.getElementById("password").value;
		if(reg_password.length == 0) {
			mui.toast("密码不能为空");
			return;
		}
		if(reg_password.length < 6) {
			mui.toast("密码长度不能少于6位！");
			return;
		}
		if(reg_password.length > 16) {
			mui.toast("密码长度不能超过16位！");
			return;
		}
		mtime(getChecknum);	
		$.ajax({
			type: 'get',
			url:httpAdress + '/api/user/getSMSCode',
			data: {
				'phone':accountBox.value,
				'type':1,
				'validcode':0
				
			},
			contentType: "application/json; charset=utf-8",
			dataType: 'jsonp',
			jsonp: "callback",
			jsonpCallback: "successCallback",
			success: function(data) {
				if(data.status==1){
					mtime(getChecknum);	
				}
				

				
//				getChecknum.style.background = 'gray';
//				   var mydata = JSON.parse(data);
//				   console.log(mydata.status);
					mui.toast(data.msg);
				
			},
			error: function(error) {
				//	                        $("#mask").fadeOut();
				//	                        $("#aloading").css("display", "none");
				mui.toast("获取失败，请重试！");
				getChecknum.style.background = '#F26100';
			}
		})
	})
		
	regButton.addEventListener('tap', function(event) {
		var network = true;
		if(network) {
			if(accountBox == "") {
				mui.toast("手机号不能为空");
				return;
			}
			var check_phone_number = /^1([38]\d|4[57]|5[0-35-9]|7[06-8]|8[89])\d{8}$/;
			if(accountBox.value.length == 0) {
				mui.toast("手机号不能为空");
				return;
			}
			if(accountBox.value.length != 11) {
				mui.toast("请输入有效的手机号！");
				return;
			}
			if(!accountBox.value.match(check_phone_number)) {
				mui.toast("请输入有效的手机号！");
				return;
			}
		}
		//密码校验
		var reg_password = document.getElementById("password").value;
		if(reg_password.length == 0) {
			mui.toast("密码不能为空");
			return;
		}
//		if(pagenum.value.length == 0) {
//			mui.toast("请输入图形码");
//			return;
//		}
		if(checknum.value.length == 0) {
			mui.toast("请输入验证码");
			return;
		}
//		if(please.value.length == 0) {
//			mui.toast("请输入邀请码");
//			return;
//		}
		if(reg_password.length < 6) {
			mui.toast("密码长度不能少于6位！");
			return;
		}
		if(reg_password.length > 16) {
			mui.toast("密码长度不能超过16位！");
			return;
		}else{
			var ss = localStorage.getItem("abc");
			console.log(ss);
			
			var data = {};
			data["phone"] = accountBox.value;
			data["passwd"] = reg_password;
			data["channel"] = 3;
			data["checknum"] = checknum.value;
			data["type"] =1;
			data["abc"] = ss;
			data["validcode"] = 0;
			console.log(data);
//			alert(ss);
			$.ajax({
			type: 'get',
			url: httpAdress + '/api/user/register',
			data: data,
			contentType: "application/json; charset=utf-8",
			dataType: 'jsonp',
			jsonp: "callback",
			timeout: 5000,
			jsonpCallback: "successCallback",
			success: function(data) {
				console.log(data.status);
				if(data.status==1){
				  mui.toast("注册成功！");
				  
				  localStorage.setItem("userid",data.userid);
				  localStorage.setItem("user", accountBox.value);
				  window.location.href='../home/home.html';
				}
else{
					mui.toast("注册失败，请重试!");
				}
			},
			error: function(error) {
				mui.toast("注册失败，请重试！");
			}
		});	
		}

		//
		//		window.addEventListener('resize', function() {
		//			oauthArea.style.display = document.body.clientHeight > 400 ? 'block' : 'none';
		//		}, false);

	});
	console.log(localStorage.getItem("abc"))
	
